package org.xbrlapi.impl;

import org.apache.log4j.Logger;
import org.xbrlapi.AspectValuePair;
import org.xbrlapi.Fragment;
import org.xbrlapi.aspects.AspectValue;
import org.xbrlapi.builder.BuilderImpl;
import org.xbrlapi.utilities.XBRLException;

/**
 * @author Geoffrey Shuetrim (geoff@galexy.net)
 */

public class AspectValuePairImpl extends NonFragmentXMLImpl implements AspectValuePair {

    /**
     *
     */
    private static final long serialVersionUID = 9141633045533213712L;

    private static final Logger logger = Logger.getLogger(AspectValuePairImpl.class);

    /**
     * @throws XBRLException
     */
    public AspectValuePairImpl() throws XBRLException {
        super();
    }

    public AspectValuePairImpl(AspectValue aspectValue, Fragment fragment) throws XBRLException {
        this();
        setBuilder(new BuilderImpl());
        if (aspectValue == null) throw new XBRLException("The aspect value must not be null.");
        if (fragment == null) throw new XBRLException("The fragment must not be null.");
        String id = fragment.getStore().getId(aspectValue.getAspectId() + aspectValue.getId() + fragment.getIndex());
        this.setIndex(id);
        this.setFragmentIndex(fragment.getIndex());
        this.setFragmentType(fragment.getType());
        this.setAspectId(aspectValue.getAspectId());
        this.setAspectValueId(aspectValue.getId());
    }

    /**
     * @see AspectValuePair#getFragmentIndex()
     */
    public String getFragmentIndex() throws XBRLException {
        return this.getMetaAttribute("fragmentIndex");
    }

    /**
     * @param fragmentIndex The index of the fragment with this aspect value.
     * @throws XBRLException if a parameter is null.
     */
    private void setFragmentIndex(String fragmentIndex) throws XBRLException {
        if (fragmentIndex == null) throw new XBRLException("The fragment index must not be null.");
        this.setMetaAttribute("fragmentIndex", fragmentIndex);
    }

    /**
     * @see AspectValuePair#getFragmentType()
     */
    public String getFragmentType() throws XBRLException {
        return this.getMetaAttribute("fragmentType");
    }

    /**
     * @param fragmentType The type of the fragment with this aspect value.
     * @throws XBRLException if a parameter is null.
     */
    private void setFragmentType(String fragmentType) throws XBRLException {
        if (fragmentType == null) throw new XBRLException("The fragment type must not be null.");
        this.setMetaAttribute("fragmentType", fragmentType);
    }

    /**
     * @see AspectValuePair#getAspectId()
     */
    public String getAspectId() throws XBRLException {
        return this.getMetaAttribute("aspectId");
    }

    /**
     * @param aspectId The aspect ID
     * @throws XBRLException if a parameter is null.
     */
    private void setAspectId(String aspectId) throws XBRLException {
        if (aspectId == null) throw new XBRLException("The aspectId must not be null.");
        this.setMetaAttribute("aspectId", aspectId);
    }

    /**
     * @see AspectValuePair#getAspectValueId()
     */
    public String getAspectValueId() throws XBRLException {
        return this.getMetaAttribute("aspectValueId");
    }

    /**
     * @param valueId The aspect value ID
     * @throws XBRLException if a parameter is null.
     */
    private void setAspectValueId(String valueId) throws XBRLException {
        if (valueId == null) throw new XBRLException("The aspect value ID must not be null.");
        this.setMetaAttribute("aspectValueId", valueId);
    }

    /**
     * @see AspectValuePair#getFragment()
     */
    public Fragment getFragment() throws XBRLException {
        return this.getStore().<Fragment>getXMLResource(getFragmentIndex());
    }

}
